using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class HopDongRepository
	{
		public static List<HopDong> SelectAll()
		{
			return DataContext.Instance.HopDongs.OrderBy(item => item.TenHopDong).ToList();
		}

		public static HopDong SelectByID(int mahopdong)
		{
			return DataContext.Instance.HopDongs.FirstOrDefault(item => item.MaHopDong == mahopdong );
		}

		public static bool Insert(HopDong obj)
		{
			try
			{
				DataContext.Instance.HopDongs.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int mahopdong)
		{
			try
			{
				var delitem = DataContext.Instance.HopDongs.FirstOrDefault(item => item.MaHopDong == mahopdong );
				DataContext.Instance.HopDongs.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<HopDong> RetrieveByID(int mahopdong)
		{
			return (from item in DataContext.Instance.HopDongs where  item.MaHopDong == mahopdong  select item).ToList();
		}

		public static List<HopDong> SelectByMaKhachHang(int makhachhang)
		{
			var lstItem = (from item in DataContext.Instance.HopDongs where item.MaKhachHang == makhachhang select item).ToList();
			return lstItem;
		}

		public static List<HopDong> SelectByMaCongTrinh(int macongtrinh)
		{
			var lstItem = (from item in DataContext.Instance.HopDongs where item.MaCongTrinh == macongtrinh select item).ToList();
			return lstItem;
		}

	}
}